package com.guoheng.safe.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.safe.common.base.Result;
import com.guoheng.safe.common.constant.CommonConstants;
import com.guoheng.safe.common.enums.ErrorCodeEnum;
import com.guoheng.safe.sys.dto.IdDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlThresholdAddDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlThresholdPageDTO;
import com.guoheng.safe.sys.dto.basicinfo.ControlThresholdUpdateDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.guoheng.safe.sys.mapper.basicinfo.ControlThresholdMapper;
import com.guoheng.safe.sys.model.basicinfo.ControlThreshold;
import com.guoheng.safe.sys.service.ControlThresholdService;

import java.util.List;
/**
 * 类功能描述:　ControlThresholdServiceImpl
 *
 * @author Eternal
 * @date 2019-38-19 17:38
 */
@Service
public class ControlThresholdServiceImpl implements ControlThresholdService {

    @Resource
    private ControlThresholdMapper controlThresholdMapper;


    @Override
    public Result addControlThreshold(ControlThresholdAddDTO controlThresholdAddDTO) {
        ControlThreshold data = controlThresholdMapper.selectOne(controlThresholdAddDTO.getControlType(), controlThresholdAddDTO.getSensorNo());
        if(null != data){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DUPLICATED_DATA);
        }
        ControlThreshold controlThreshold = new ControlThreshold();
        BeanUtils.copyProperties(controlThresholdAddDTO,controlThreshold);
        int result = controlThresholdMapper.insertSelective(controlThreshold);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_CREATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result updateControlThreshold(ControlThresholdUpdateDTO controlThresholdUpdateDTO) {
        ControlThreshold controlThreshold = new ControlThreshold();
        BeanUtils.copyProperties(controlThresholdUpdateDTO,controlThreshold);
        int result = controlThresholdMapper.updateByPrimaryKeySelective(controlThreshold);
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_UPDATE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result deleteControlThreshold(IdDTO idDTO) {
        int result = controlThresholdMapper.deleteByPrimaryKey(idDTO.getId());
        if (CommonConstants.DataBaseOperationStatus.IS_FAILURE == result){
            return Result.failure(ErrorCodeEnum.SYS_ERR_DELETE_FAILED);
        }
        return Result.success();
    }

    @Override
    public Result getControlThreshold(IdDTO idDTO) {
        ControlThreshold controlThreshold = controlThresholdMapper.selectByPrimaryKey(idDTO.getId());
        if (null == controlThreshold){
            return Result.failure(ErrorCodeEnum.SYS_ERR_SEARCH_FAILED);
        }
        return Result.success(controlThreshold);
    }

    @Override
    public Result getControlThresholdPage(ControlThresholdPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum() , pageDTO.getPageSize());
        Integer controlType = pageDTO.getFilters().getInteger("controlType");
        List<ControlThreshold> controlThresholds = controlThresholdMapper.selectAll(controlType);
        PageInfo<ControlThreshold> pageInfo = new PageInfo<>(controlThresholds);
        return Result.success(pageInfo);
    }
}

